package com.requiem.hist_group_manage_springboot.service.impl;

import com.requiem.hist_group_manage_springboot.common.HttpParamUtil;
import com.requiem.hist_group_manage_springboot.common.PageUtil;
import com.requiem.hist_group_manage_springboot.constants.SystemConstants;
import com.requiem.hist_group_manage_springboot.exception.R;
import com.requiem.hist_group_manage_springboot.mapper.BlackMapper;
import com.requiem.hist_group_manage_springboot.mapper.StudentMapper;
import com.requiem.hist_group_manage_springboot.model.BlackModel;
import com.requiem.hist_group_manage_springboot.model.StudentModel;
import com.requiem.hist_group_manage_springboot.service.BlackService;
import com.requiem.hist_group_manage_springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class BlackServiceImpl implements BlackService {
    @Autowired
    BlackMapper blackMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    StudentService studentService;
    @Autowired
    HttpParamUtil httpParamUtil;
    @Autowired
    PageUtil pageUtil;

    @Override
    public R getList() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> blackMapper.getList());
        List<?> dataList = (List<?>) result.get("list");
        return R.success(result);
    }

    @Override
    public R add(BlackModel blackModel) {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int studentId = Integer.parseInt(params.get("studentId"));
        String groupIdStr = params.get("groupId");
        Integer groupId;
        if (groupIdStr != null) {
            groupId = Integer.parseInt(groupIdStr);
            // 后续操作
        } else {
            groupId = null;
        }
        if (groupId != null) {
            studentService.exitGroup(studentId, groupId);
        }
        BlackModel byID = blackMapper.getByID(blackModel.getStudentId());
        if (byID != null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_BLACK_MSG);
        }
        LocalDateTime now = LocalDateTime.now();
        blackModel.setCreateTime(now);
        int add = blackMapper.add(blackModel);
        if (add != 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        StudentModel studentByID = studentMapper.getStudentByID(blackModel.getStudentId());
        if (studentByID == null) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        studentByID.setBlackType(1);
        int i = studentMapper.editStudent(studentByID);
        if (i != 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    @Override
    public R delete() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int studentId = Integer.parseInt(params.get("studentId"));
        BlackModel byID = blackMapper.getByID(studentId);
        if (byID == null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_BLACK_MSG);
        }
        int delete = blackMapper.delete(studentId);
        if (delete != 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        StudentModel studentByID = studentMapper.getStudentByID(studentId);
        if (studentByID == null) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        studentByID.setBlackType(0);
        int i = studentMapper.editStudent(studentByID);
        if (i != 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    @Override
    public R SearchList(BlackModel blackModel) {
        Map<String, String> params = httpParamUtil.parseQueryString();
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> blackMapper.getSearchList(blackModel));
        return R.success(result);
    }
}
